package com.woniuxy.credit_loans.accounting_shi.service.impl;

import com.woniuxy.credit_loans.accounting_shi.service.AccountService;
import com.woniuxy.credit_loans.accounting_shi.vo.WeiDaVo;
import com.woniuxy.credit_loans.entity.*;
import com.woniuxy.credit_loans.mapper.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class AccountServiceImpl implements AccountService {
    @Resource
    private PipelineMapper pipelineMapper;
    @Resource
    private AccountMapper accountMapper;
    @Resource
    private DebitInfoMapper debitInfoMapper;
    @Resource
    private RepaymentMapper repaymentMapper;
    @Resource
    private BorrowMapper borrowMapper;
    @Override
    public Account selectAccountGongBank(Long id, WeiDaVo weiDaVo) {
        return accountMapper.selectAccountGongBank(id,weiDaVo);
    }

    @Override
    public List<Account> selectAll() {
        return accountMapper.selectAll();
    }

    @Override
    public int insertSelective(Account record) {
        return accountMapper.insertSelective(record);
    }

    @Override
    @Transactional
        public Account selectAccountBankAccount(Long id,WeiDaVo weiDaVo) {
        Account account = accountMapper.selectAccountBankAccount(id, weiDaVo);
        account.setAccountBanlance(account.getAccountBanlance().add(weiDaVo.getLiLoanMoney()));
        accountMapper.updateByPrimaryKeySelective2(account);
        DebitInfo debitInfo=new DebitInfo();
        debitInfo.setDiDebitStatus("还款中");
        debitInfo.setDiNumber(id.intValue());
        debitInfoMapper.updateByPrimaryKeySelective(debitInfo);
        Account account1 = accountMapper.selectAccountGongBank(id, weiDaVo);
        account1.setAccountBanlance(account1.getAccountBanlance().subtract(weiDaVo.getLiLoanMoney()));
        account1.setAccountOutlay(account1.getAccountOutlay().add(weiDaVo.getLiLoanMoney()));
        Pipeline pipeline=new Pipeline();
        pipeline.setPipelineInorout("转出");
        pipeline.setPipelineBy1(account1.getAccountNo());
        pipeline.setPipelineBy2(weiDaVo.getLiLoanMoney().toString());
        pipeline.setPipelineType("转账流水");
        pipelineMapper.insertSelective(pipeline);
        accountMapper.updateByPrimaryKeySelective2(account1);
        Repayment repayment=new Repayment();
        repayment.setBy1("未还款");
        repayment.setHeTongId(Integer.parseInt(weiDaVo.getId()));
        repayment.setSumExpirse(weiDaVo.getDiTimeLimit());
        Date date = new Date();
        date.setDate(28);
        date.setMonth(date.getMonth()+1);
        repayment.setLimitingDate(date);
        BigDecimal multiply = weiDaVo.getLiLoanMoney().multiply(weiDaVo.getAllInRate());
        BigDecimal add = weiDaVo.getLiLoanMoney().add(multiply);
        BigDecimal divid = add.divide(BigDecimal.valueOf(weiDaVo.getDiTimeLimit()),BigDecimal.ROUND_CEILING);
        repayment.setCurrentSum(divid);
        repayment.setNowExpirse(1);
        BigDecimal subtrac = divid.subtract(multiply);
        repayment.setCurrentInterest(subtrac);
        repaymentMapper.insertSelective(repayment);
        return account;
    }

    @Override
    public int updateByPrimaryKeySelective(Account record) {
        return accountMapper.updateByPrimaryKeySelective(record);
    }
}
